package com.friday.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.friday.dao.InquiryDao;
import com.friday.dao.QuotationDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class QuotationController {

    @Autowired
    public QuotationDao quotationDao;
    @Autowired
    public InquiryDao inquiryDao;

    @RequestMapping("/quotationList")
    public JSONArray getAllQuotation(){
        return quotationDao.getAllQuotation();
    }

    @RequestMapping("/quotationAgentList")
    public JSONArray getAgentQuotation(@RequestParam("id") int id){
        return quotationDao.getAgentQuotation(id);
    }

    @RequestMapping("/quotationDetail")
    public JSONObject getQuotationAndDetail(@RequestParam("id") int id){
        JSONObject result=quotationDao.getQuotationById(id);
        JSONArray goods = quotationDao.getQuotationdtlById(id);
        for(int i=0;i<goods.size();i++){
            goods.getJSONObject(i).put("key",i);
        }
        result.put("goods",goods);
        return result;
    }

    @RequestMapping("/quotationAdd")
    public JSONObject addQuotation(@RequestBody JSONObject message){
        JSONObject result = new JSONObject();
        //预置错误
        result.put("result","fail");
        result.put("quotation_id",null);
        int tag1 = quotationDao.addQuotation(message);
        //报价单插入成功
        if(tag1>0 ){
            int quotation_id=message.getInteger("id");
            int tag2 = 0;
            JSONArray quotdtl=message.getJSONArray("goods");
            //添加报价单详情
            for(int i=0;i<quotdtl.size()-1;i++){
                quotdtl.getJSONObject(i).put("quotation_id",quotation_id);
                tag2+=quotationDao.addQuotationDtl(quotdtl.getJSONObject(i));
            }
            //报价单详情插入成功
            if(tag2==quotdtl.size()-1){
                result.put("result","success");
                result.put("quotation_id",quotation_id);
                //若参考询价单，则将询价单状态修改为已完成
                if(message.getInteger("inquiry_id")!=null){
                    inquiryDao.changeStatus(message.getInteger("inquiry_id"),"已完成");
                }
            }
        }
        return result;
    }

    @RequestMapping("/quotationAddNoRef")
    public JSONObject addQuotationNoRef(@RequestBody JSONObject message){
        JSONObject result = new JSONObject();
        //预置错误
        result.put("result","fail");
        result.put("quotation_id",null);
        int tag1 = quotationDao.addQuotation(message);
        //报价单插入成功
        if(tag1>0 ){
            int quotation_id=message.getInteger("id");
            int tag2 = 0;
            JSONArray quotdtl=message.getJSONArray("goods");
            //添加报价单详情
            for(int i=0;i<quotdtl.size();i++){
                quotdtl.getJSONObject(i).put("quotation_id",quotation_id);
                tag2+=quotationDao.addQuotationDtl(quotdtl.getJSONObject(i));
            }
            //报价单详情插入成功
            if(tag2==quotdtl.size()){
                result.put("result","success");
                result.put("quotation_id",quotation_id);
                //若参考询价单，则将询价单状态修改为已完成
                if(message.getInteger("inquiry_id")!=null){
                    inquiryDao.changeStatus(message.getInteger("inquiry_id"),"已完成");
                }
            }
        }
        return result;
    }

    @RequestMapping("/quotationSend")
    public String sendQuotation(@RequestParam("id") int id){
        int tag = quotationDao.changeStatus(id,"待回复");
        if(tag>0)
            return "success";
        return "fail";
    }

    @RequestMapping("/quotationReply")
    public String changeQuotation(@RequestParam("id") int id,@RequestParam("status") String status){
        int tag=quotationDao.changeStatus(id,status);
        if(tag>0)
            return "success";
        return "fail";
    }


    @RequestMapping("/quotationDelete")
    public String delQuotation(@RequestParam("id") int id){
        JSONObject quotation=quotationDao.getQuotationById(id);
        //删除报价单及相应详情单
        int result1=quotationDao.delQuotationById(id);
        int result2=quotationDao.delQuotationDtlById(id);
        //若删除报价单，则对应询价单状态回退为待回复
        if(quotation.getInteger("inquiry_id")!=null){
            inquiryDao.changeStatus(quotation.getInteger("inquiry_id"),"待回复");
        }
        if(result1>0 && result2>0)
            return "success";
        return "fail";
    }

    @RequestMapping("/quotationEdit")
    public String editQuotation(@RequestBody JSONObject quotation){
        int tag1= quotationDao.editQuotation(quotation);
        int tag2=0;

        int quotation_id=quotation.getInteger("id");
        //删除原来的quotationdtl
        int tag3=quotationDao.delQuotationDtlById(quotation_id);
        //获取新的quotationdtl
        JSONArray dtl=quotation.getJSONArray("goods");
        int size=dtl.size();
        //若是有参考报价单，则去掉最后一行
        if(quotation.containsKey("inquiry_id"))
            size--;
        System.out.println(size);
        for(int i=0;i<size;i++){
            //添加新的quotationdtl
            dtl.getJSONObject(i).put("quotation_id",quotation_id);
            tag2+=quotationDao.addQuotationDtl(dtl.getJSONObject(i));
        }
        if(tag1>0 && tag2==size)
            return "success";
        return "fail";
    }



    @RequestMapping("/quotationMatch")
    public JSONArray matchQuotation(@RequestParam("id") int id,@RequestParam("authority") String authority){
        return quotationDao.getAllMatchQuotation(id,authority);
    }


}
